1 module hip.gui.label; 2 public import hip.gui.widget; 3 public import hip.api.graphics.text; 4 public import hip.api.graphics.color; 5 import hip.game2d.text; 6 7 class Label : Widget 8 { 9 protected HipText txtDraw; 10 protected IWidgetRenderer bkgRenderer; 11 12 this(string text, HipColor bkgColor = HipColor(127,127,127,127)) 13 { 14 this.txtDraw = new HipText(); 15 this.txtDraw.wordWrap = true; 16 this.text = text; 17 this.bkgRenderer = new DebugWidgetRenderer(bkgColor); 18 setAlign(HipTextAlign.LEFT, HipTextAlign.CENTER); 19 } 20 21 public void setBackgroundRenderer(IWidgetRenderer bkgRenderer) 22 { 23 this.bkgRenderer = bkgRenderer; 24 } 25 public string text(string text) 26 { 27 txtDraw.text = text; 28 txtDraw.getSize(this.width, this.height); 29 return text; 30 } 31 public string text(){return txtDraw.text;} 32 33 public bool wordWrap(){return txtDraw.wordWrap;} 34 public bool wordWrap(bool bWordWrap){return txtDraw.wordWrap = bWordWrap;} 35 36 public void setAlign(HipTextAlign horizontal, HipTextAlign vertical) 37 { 38 txtDraw.setAlign(horizontal,vertical); 39 txtDraw.getSize(this.width, this.height); 40 } 41 42 public void setSize(int width, int height) 43 { 44 txtDraw.boundsWidth = width; 45 txtDraw.boundsHeight = height; 46 txtDraw.getSize(this.width, this.height); 47 } 48 private void getTextPosition(out int x, out int y) 49 { 50 getPositionFromAlignment( 51 worldTransform.x, worldTransform.y, 52 txtDraw.width, txtDraw.height, 53 txtDraw.alignh, txtDraw.alignv, 54 x, y, 55 txtDraw.boundsWidth, txtDraw.boundsHeight 56 ); 57 } 58 59 override void onRender() 60 { 61 int bkgX = void, bkgY = void; 62 getTextPosition(bkgX, bkgY); 63 txtDraw.setPosition(worldTransform.x, worldTransform.y+txtDraw.height/4); 64 if(bkgRenderer !is null) bkgRenderer.render(bkgX, bkgY, txtDraw.width, txtDraw.height); 65 txtDraw.draw(); 66 } 67 }